package com.ruijie.acl.web;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.international.StatusMessage.Severity;

import com.ruijie.acl.TimeRange;
import com.ruijie.acl.service.DeviceTimeRangeService;
import com.ruijie.acl.service.TimeRangeService;
import com.ruijie.acl.utils.AclUtil;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.exception.BusinessException;
import com.ruijie.foundation.service.Repository;
import com.ruijie.foundation.web.intercept.AddSuccessInfo;

/**
 * <p>
 * Title: ACL时间列表管理Action类
 * 
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * 为ACL时间列表管理模块提供页面数据的显示、获取和页面的跳转。
 * 
 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time:
 * </p>
 * <p>
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * 
 * @author ASAI
 */

@Name("timeAbsoluteAction")
@Scope(ScopeType.CONVERSATION)
@AddSuccessInfo
public class TimeAbsoluteAction extends AclBaseAction {
	
	@In("#{timeRangeService}")
	private TimeRangeService timeRangeService;

	@In("#{deviceTimeRangeService}")
	private DeviceTimeRangeService deviceTimeRangeService;
	
	private TimeRange model;
	
	private TimeRange criteria = new TimeRange();
	
	private Date preloadBeginDate;
	
	private Date preloadEndDate;
	
	@In("#{timeRangeAction}")
	private TimeRangeAction timeRangeAction;
	
	public Date getPreloadBeginDate() {
		DateFormat dateFormat = DateFormat.getDateInstance();
		try {
			preloadBeginDate = dateFormat.parse("1993-01-01");
		} catch (ParseException e) {
			preloadBeginDate = new Date();
		}
		return preloadBeginDate;
	}

	public Date getPreloadEndDate() {
		DateFormat dateFormat = DateFormat.getDateInstance();
		try {
			preloadEndDate = dateFormat.parse("2035-12-31");
		} catch (ParseException e) {
			preloadEndDate = new Date();
		}
		return preloadEndDate;
	}

	@Override
	protected boolean doAdd(){
		TimeRange timeRange = (TimeRange) this.getParentEntity();
		if(model.getStartDate()!=null){
			timeRange.setStartDate(model.getStartDate());
		}
		if(model.getEndDate()!=null){
			timeRange.setEndDate(model.getEndDate());
		}
		
		// 检测结束时间不能大于开始时间
		if(!this.checkTime()){
			return false;
		}
		if ((timeRange.getStartDate() != null || timeRange.getEndDate()!=null) && timeRange.getId()!= null) {
			TimeRange timeRangeTemp = timeRangeService.get(timeRange.getId());
			// 没有变更
			if(AclUtil.equals(timeRangeTemp.getStartDate(), timeRange.getStartDate())
					&& AclUtil.equals(timeRangeTemp.getEndDate(), timeRange.getEndDate())){
				this.setParentEntity(timeRange);
				timeRangeAction.setModel(timeRange);
				return true;
			}else{//变更
				timeRangeTemp.setStartDate(timeRange.getStartDate());
				timeRangeTemp.setEndDate(timeRange.getEndDate());
				if (this.isRelativeDevice(timeRangeTemp)) {
					// 更新"变更警示"
					timeRangeTemp.setIsChanged(true);
					deviceTimeRangeService.updateChangedByTimeRange(timeRangeTemp);
				}
				timeRangeService.update(timeRangeTemp);
				this.setParentEntity(timeRangeTemp);
				timeRangeAction.setModel(timeRangeTemp);
				return true;
			}
		}
		this.setParentEntity(timeRange);
		return true;
	}
	
	@Override
	protected boolean doUpdate() {
		return this.doAdd();
	}
	
    public void deleteTimeAbsolute(TimeRange timeRange){
    	timeRange.setStartDate(null);
    	timeRange.setEndDate(null);
    	if (this.isRelativeDevice(timeRange)) {
    		timeRange.setIsChanged(true);
    		deviceTimeRangeService.updateChangedByTimeRange(timeRange);
    	}
		timeRangeService.update(timeRange);
    	//redirectTo("/acl/timeRangeDetail.seam");
		addGlobalI18nMessage("crud_del_success_msg");
	}
	
	/**
	 * 开始时间不能大于或者等于结束时间
	 */
	public boolean checkTime() {	
		if(model.getStartDate()==null && model.getEndDate()==null){
			//throw new BusinessException("acl_timeAbsolute_info_notNull");
			this.addGlobalI18nMessage(Severity.ERROR, "acl_timeAbsolute_info_notNull");
			return false;
		}
		if(model.getStartDate()!=null && model.getEndDate()!=null){
			Calendar startCalendar = Calendar.getInstance();
			startCalendar.setTime(this.model.getStartDate());
			Calendar endCalendar = Calendar.getInstance();
			endCalendar.setTime(this.model.getEndDate());
			// 开始时间不能大于或者等于结束时间
			if (startCalendar.compareTo(endCalendar) >= 0) {
				//throw new BusinessException("acl_link_used_error_single");
				this.addGlobalI18nMessage(Severity.ERROR, "acl_link_used_error_single");
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * 表示是否修改 如是Model不存在，或者不是更新，则返回TRUE
	 * @return
	 */
	public boolean checkChanged() {
		if (model != null && model.getId() != null) {
			TimeRange timeRange = timeRangeService.get(model.getId());
			if(AclUtil.equals(timeRange.getStartDate(), model.getStartDate())
					&& AclUtil.equals(timeRange.getEndDate(), model.getEndDate())){
				return false;
			}else{
				timeRange = model.clone(timeRange, null);
				model = timeRange;
			}
		}
		return true;
	}
	
	/**
	 * 是否已经被设备关联。
	 * 
	 * @return 是否关联
	 */
	public boolean isRelativeDevice(TimeRange timeRange) {
		return deviceTimeRangeService.isExistDeviceTimeRange(null, timeRange);
	}
	
	public String toUpdatePage(TimeRange timeRange){
		this.setParentEntity(timeRange);
		this.setModel(timeRange);
		return getUpdatePage();
	}

	
	@SuppressWarnings("unchecked")
	@Override
	protected Repository getRepository() {
		return this.timeRangeService;
	}

	public String toAddPage(TimeRange timeRange) {
		this.setParentEntity(timeRange);
		timeRange.setStartDate(null);
		timeRange.setEndDate(null);
		this.setModel(timeRange);
		
		return getAddPage();
	}

	@Override
	protected String getAddPage() {
		return "/acl/timeAbsoluteAdd.seam";
	}

	@Override
	public EntityObject getCriteria() {
		return this.criteria;
	}

	@Override
	protected String getDetailPage() {
		return "/acl/timeRangeDetail.seam";
	}

	@Override
	protected String getListPage() {
		return "/acl/timeRangeDetail.seam";
	}

	@Override
	public EntityObject getModel() {
		return this.model;
	}

	@Override
	protected String getUpdatePage() {
		return "/acl/timeAbsoluteEdit.seam";
	}

	@Override
	public void resetModel() {
		this.setModel(this.getRepository().get(model.getId()));
		timeRangeAction.setModel(model);
	}
	

	@Override
	public void setCriteria(EntityObject criteria) {
		this.criteria=(TimeRange)criteria;
	}

	@Override
	public void setModel(EntityObject model) {
		this.model=(TimeRange)model;
	}
	
	//-------------modify by sinyee 2010-10-22 being----------------

	public String toAddPageBlank(TimeRange timeRange){
		this.toAddPage(timeRange);
		return null;
	}
	
	@AddSuccessInfo
	public String addEntity(){
		this.add();
		return null;
	}

	public String toUpdatePageBlank(TimeRange timeRange){
		this.toUpdatePage(timeRange);
		return null;
	}
	
	@AddSuccessInfo
	public String updateEntity(){
		this.update();
		return null;
	}
	
	//--------------modify by sinyee 2010-10-22 end----------------
	
}
